package com.example.libraryapp.user;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.libraryapp.R;
import com.example.libraryapp.activity.MainActivity;
import com.example.libraryapp.book.BookService;
import com.example.libraryapp.util.MyConfig;
import com.example.libraryapp.util.Validator;

/**
 * 用户登录
 */
public class LoginActivity extends Activity {
	private static final String TAG = "LoginActivity";

	private EditText etUsername;
	private EditText etPassword;
	private Button bnLogin; // 登录按钮
	private TextView tvRegister; // 注册
	private TextView tvLoginType; // 登陆类型

	private String key = "";
	private Activity mActivity;
	private static Context mContext;
	private String backTitle; // 返回标题
	private static final String TITLE = "登录";

	private String username; // 用户名
	private String password; // 密码

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		init();
		findViews();
		setListeners();
	}

	/**
	 * 初始化参数
	 */
	private void init() {
		this.key += this.hashCode();
		mActivity = LoginActivity.this;
		mContext = LoginActivity.this;
		//清理数据库
	//	UserService.clear(mContext);

	}

	/**
	 * 找到控件
	 */
	private void findViews() {
		// 标题栏
		// 登录
		etUsername = (EditText) this.findViewById(R.id.et_username);
		etPassword = (EditText) this.findViewById(R.id.et_password);
		bnLogin = (Button) this.findViewById(R.id.bn_login);
		// 注册，忘记密码
		tvRegister = (TextView) this.findViewById(R.id.tv_register);
	}

	/**
	 * 处理注册
	 * 
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data != null) {
			UserInfo userInfo = data.getExtras().getParcelable("user");
			if (userInfo != null) {
				etUsername.setText(userInfo.getCardid());
				// etPassword.setText(password);
				// userLogin(username, password);
			}
		}
	}

	/**
	 * 设置监听
	 */
	private void setListeners() {
		// 登录
		bnLogin.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				username = etUsername.getText().toString().trim();
				password = etPassword.getText().toString().trim();
				// 用户登录
				userLogin(username, password);
			}
		});
		// 跳转到用户注册页面
		tvRegister.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(mContext, RegisterActivity.class);
				startActivityForResult(intent, 1);
			}
		});
	}

	/**
	 * 用户登录
	 */
	private void userLogin(String cardid, String password) {
		if (!Validator.isEffective(cardid)) {
			Toast.makeText(mContext, "用户名不能为空", 1).show();
			return;
		}
		if (!Validator.isEffective(password)) {
			Toast.makeText(mContext, "密码不能为空！", 1).show();
			return;
		}
		UserInfo userInfo = UserService.login(mContext, cardid, password);
		if (userInfo != null) {
			Toast.makeText(mContext, "登陆成功！", 1).show();
			loginSuccess(userInfo);
		} else {
			Toast.makeText(mContext, "登陆失败！", 1).show();
		}
	}

	/**
	 * 登录成功
	 */
	private void loginSuccess(UserInfo userinfo) {
		if (userinfo != null) { // 把 用户信息保存起来
			MyConfig.setUserInfo(mContext, userinfo);
		}
		Intent intent = new Intent(mContext, MainActivity.class);
		startActivity(intent);
		finish();
	}

}
